package com.shmet.tool.localcalltwo.frame.context;

import com.shmet.tool.localcalltwo.frame.base.BusService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author shmet
 * @date 2022/7/28 15:46
 * @desc 类接口注册
 */
@Component
public class BeanContext implements ApplicationContextAware {

    private static Map<String, BusService> instanceRegisterMap = new HashMap<>();

    public static BusService getRegBean(String name) {
        return instanceRegisterMap.get(name);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 注册实例
        Map<String, BusService> objectRegMap = applicationContext.getBeansOfType(BusService.class);
        for (BusService instance : objectRegMap.values()) {
            String beanName = instance.getClass().getSimpleName().replace("Impl", "");
            beanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
            instanceRegisterMap.put(beanName, instance);
        }
    }
}